package com.moyou.weichat;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.moyou.weichat.chat.ChatManger;
import com.moyou.weichat.listener.HxErroHandler;
import com.moyou.weichat.listener.TextWatcherListener;
import com.moyou.weichat.utils.ConstantUtils;
import com.moyou.weichat.utils.EditTextUtils;
import com.moyou.weichat.utils.T;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * sj ：2017年10月16日11:03:11
 * name: ts
 * 用途:用户登录页面
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.title_name)
    TextView titleName;
    @Bind(R.id.title_left_view)
    ImageView titleLeftView;
    @Bind(R.id.melogin_et_phone)
    EditText loginUserName;
    @Bind(R.id.melogin_et_pwd)
    EditText loginUserPass;
    @Bind(R.id.melogin_iv_clearphone)
    ImageView meloginIvClearphone;
    @Bind(R.id.melogin_tv_isshowpwd)
    TextView meloginTvIsshowpwd;
    @Bind(R.id.melogin_bu_login)
    Button meloginBuLogin;
    @Bind(R.id.melogin_tv_forgetpwd)
    TextView tv_regist;

    private HxErroHandler hxErroHandler; //登录失败的回调


    @Override
    protected int getLayout() {
        return R.layout.activity_location;
    }

    @Override
    protected void initTitle() {
        titleName.setText("登录");
        titleLeftView.setImageResource(R.mipmap.ic_return);
        titleLeftView.setVisibility(View.GONE);
        EditTextUtils.getEditTextUtils().setEditTextInhibitInputSpeChat(loginUserName); //限制特殊字符！
        EditTextUtils.getEditTextUtils().setEditTextInputNuamber(loginUserName, 11, mContext, "yes", null);
    }

    @Override
    protected void initListener() {
        loginUserName.addTextChangedListener(new TextWatcherListener(loginUserPass,meloginBuLogin, ConstantUtils.LOGIN_TYPE_NAME));
        loginUserPass.addTextChangedListener(new TextWatcherListener(loginUserName,meloginBuLogin, ConstantUtils.LOGIN_TYPE_PASS));
    }

    @Override
    protected void initEventAndData() {
        hxErroHandler = new HxErroHandler(this);
    }
    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.LEFT;
    }

    @Override
    protected void createPresenter() {
//        mPresenter = new
    }


    @OnClick({R.id.melogin_bu_login,R.id.melogin_tv_forgetpwd})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.melogin_bu_login:
                dialogShow();
                String userName  = loginUserName.getText().toString();
                String userPass  = loginUserPass.getText().toString();
                if (userName.length() > 0 && userPass.length() > 0) {
                    //登录环信聊天！
                    EMClient.getInstance().login(userName, userPass, new EMCallBack() {//回调
                        @Override
                        public void onSuccess() {
                            dialogDiss();
                            hxErroHandler.sendEmptyMessage(ConstantUtils.HANDLER_SUCCESS);
                        }
                        @Override
                        public void onProgress(int progress, String status) {
                            dialogDiss();
                            hxErroHandler.sendEmptyMessage( ConstantUtils.HANDLER_LOAD);
                        }
                        @Override
                        public void onError(int code, String messages) {
                            dialogDiss();
                            Message message = new Message();
                            message.what = ConstantUtils.HANDLER_Fali;
                            message.obj = code;
                            hxErroHandler.sendMessage(message);
                        }
                    });
                } else {
                    showToast("请输入账号和密码!");
                }
                break;
            case R.id.melogin_tv_forgetpwd :
                startAct(RegisterActivity.class);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hxErroHandler = null;
    }
}
